/**
 * Created by Administrator on 2015/1/13.
 */
var m;
var n;
var show=0;
var b;
var ff = dom("div");
var $ = function (id) {
    return window.document.getElementById(id);
}
function Game(mc, nc, nb) {
    m = mc;
    n = nc;
    b=  nb;
    var ga = this;

    document.body.addEventListener("click", method(this, "leftListener"));
    document.body.oncontextmenu = function (e) {
        e.preventDefault();
        ga.righListener(e);

    }
    this.newGame(b);
}


Game.prototype.newGame = function (b) {
    ff.innerHTML = ""
    ff.appendChild(dom("H1", {id:"header"}, "Bagic"));

    var buei = [m * n];
    for (var i = 0; i < m; i++) {
        for (var j = 0; j < n; j++) {
            var img = dom("IMG");
            img.id = i + ":" + j;
            img.panduan = 0;
            img.dianji = false;//是否被点击
            img.rdianji = false;
            img.src = "Images/Cells/BlueSquare.png"
            ff.appendChild(img);
        }
        ff.appendChild(dom("br"));
        document.body.appendChild(ff);
    }
    //开始布雷
    for (var xy = 0; xy < b; xy++) {
        var x = Math.floor(Math.random() * m);
        var y = Math.floor(Math.random() * n);
        var change = $(x + ":" + y);
        if (change.panduan == "Flag") {
            xy--;
        }
        change.panduan = "Flag";

    }
    //添加数字统计雷数
    for (var i = 0; i < m; i++) {
        for (var j = 0; j < n; j++) {

            var tongji = $(i + ":" + j);
            if (tongji.panduan != "Flag") {
                var count = 0;
                //循环九个方格统计雷数
                for (var ni = i - 1; ni <= i + 1; ni++) {
                    for (var nj = j - 1; nj <= j + 1; nj++) {
                        var tongjilei = $(ni + ":" + nj);
                        if (tongjilei && (tongjilei.panduan == "Flag")) {
                            count++;
                        }
                    }
                }
                tongji.panduan = count;

            }

        }
    }
};
//左键点击
Game.prototype.leftListener = function (e) {
    if (e.target.rdianji == false) {
        var game = this;
        e.target.src = "Images/Cells/" + e.target.panduan + ".png";
        if (e.target.panduan == 0 && e.target.dianji == false) {
            var str = e.target.id;
            var a = str.split(":");
            game.saoLeiDigui(a[0], a[1]);

        }
        if (e.target.panduan == "Flag" && e.target.dianji == false) {
            stopCount();
            alert("游戏结束共用时间为："+$("time").value+"秒");
            this.gameOver();
        }
        e.target.dianji = true;
    }


}
Game.prototype.gameOver=function(){
    for (var i = 0; i < m; i++) {
        for (var j = 0; j < n; j++) {
            var allOpen = $(i + ":" + j);
            allOpen.src = "Images/Cells/" + allOpen.panduan + ".png";
            allOpen.dianji = true;
        }
    }
}

Game.prototype.righListener = function (e) {

    if (e.target.dianji == false && e.target.rdianji == false) {
        if(e.target.panduan=="Flag"){

            b--;
            if(b<1){
                stopCount();
                alert("你赢了共用时间为："+$("time").value+"秒")
                this.gameOver();
            }
        }
        console.log(b);
        e.target.src = "Images/Cells/NewFlag.png"
        e.target.rdianji = true;
        e.target.dianji = true;

    }
    else if (e.target.rdianji == true && e.target.dianji == true) {
        if(e.target.panduan=="Flag"){
            b++;
        }
        e.target.src = "Images/Cells/BlueSquare.png"
        e.target.rdianji = false;
        e.target.dianji = false;

    }


}

//递归排雷
Game.prototype.saoLeiDigui = function (x, y) {

    var diguipanduan = $(x + ":" + y);

    if (!diguipanduan || diguipanduan.panduan != 0 || diguipanduan.dianji) {
        return;
    }
    console.log(x + ":" + y);
    diguipanduan.src = "Images/Cells/0.png";
    diguipanduan.dianji = true;

    this.saoLeiDigui(x - 1, y - 1);
    this.saoLeiDigui(x - 1, y);
    this.saoLeiDigui(x - 1, y + 1);
    this.saoLeiDigui(x + 1, y - 1);
    this.saoLeiDigui(x + 1, y);
    this.saoLeiDigui(x + 1, y + 1);
    this.saoLeiDigui(x, y + 1);
    this.saoLeiDigui(x, y - 1);

}



